import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Iterator;

public class QueueDeCase
{
    private class CaseComparator implements Comparator<Case>
    {
	private static final int UNKNOW=-1;

	@Override
	    public int compare(Case c1, Case c2)
	    {
		if(c1.Fcost==UNKNOW ||c2.Fcost==UNKNOW)
		    System.err.println("c1 ou c2 n'a pas de FCOST défini : comparaison impossible");
		
		if (c1.Fcost < c2.Fcost)
		    return -1;
        
		if (c1.Fcost > c2.Fcost)
		    return 1;
        
		return 0;
	    }
    }

    Comparator<Case> comparator;
    PriorityQueue<Case> queue;

    public QueueDeCase()
    {
	 comparator = new CaseComparator();
       	 queue =new PriorityQueue<Case>(1, comparator);
    }

    public Case defiler()
    {
	Case c = queue.poll();
	return c;
    }

    public void addToQueue(Case c)
    {
	queue.add(c);
	c.isClosed=false;
	c.isOpen=true;	  
    }

    public void removeFromQueue(Case c)
    {
	Case element;
	Iterator<Case> itr=queue.iterator();
	while(itr.hasNext())  {

	    element = itr.next();
	    if(element.x==c.x && element.y==c.y) {
		queue.remove(element);
		break;
	    }
	}
	c.isOpen=false;
	c.isClosed=false;
	    
    }
}
